package com.yinhai.mycat_test.controller;

import com.yinhai.mycat_test.VO.InserOrderItemVO;
import com.yinhai.mycat_test.VO.InsertOrderVO;
import com.yinhai.mycat_test.entity.IDCard;
import com.yinhai.mycat_test.entity.Order;
import com.yinhai.mycat_test.entity.OrderItem;
import com.yinhai.mycat_test.entity.User;
import com.yinhai.mycat_test.service.TransactionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import static com.yinhai.mycat_test.service.BaseService.getOrderId;

@RestController
public class TransactionController {


    @Autowired
    private TransactionService transactionService;
    
    User getUser(int id) {
        User user = new User();
        user.setUserId(id);
        user.setUserName("gan");
        user.setAge(15);
        user.setEmail("ljnwsbs@163.com");
        user.setGender("male");
        user.setPhoneNumber("1234567890");
        return user;
    }

    IDCard getIdCard(long id) {
        IDCard idCard = new IDCard();
        idCard.setUserId(1L);
        idCard.setId(id);
        idCard.setExpireDate(System.currentTimeMillis());
        idCard.setAddress("hebei");
        idCard.setIdCard("123456789");
        return idCard;
    }

    InsertOrderVO getInsertOrderVO(long count) {
        InsertOrderVO insertOrderVO = null;
        for (int i = 0; i < 1; i++) {
            insertOrderVO = new InsertOrderVO();
            insertOrderVO.setAddress("addres:" + i);
            insertOrderVO.setTotalPrice(Float.valueOf(i));
            insertOrderVO.setUserId(i);
            List<InserOrderItemVO> orderItemVOList = new ArrayList<>();
            for (int k = 0; k < count; k++) {
                InserOrderItemVO itemVO = new InserOrderItemVO();
                itemVO.setItemCount(k);
                itemVO.setItemId(k);
                itemVO.setItemName("item name:" + k);
                itemVO.setItemPrice(Float.valueOf(i * k));
                orderItemVOList.add(itemVO);
            }
            insertOrderVO.setItemList(orderItemVOList);

        }
        return insertOrderVO;
    }
    
    Order getOrder(long count){
        Order order = new Order();
        order.setAddress("address:"+count);
        order.setUserId(count);
        order.setOrderTime(LocalDateTime.now());
        Long orderId = getOrderId();
        order.setOrderId(orderId);
        order.setTotalPrice(1.0f);
        return order;
    }
    
    OrderItem getOrderItem(long count){
        OrderItem orderItem = new OrderItem();
        orderItem.setGoodsCount(1);
        orderItem.setGoodsName("item name:"+count);
        orderItem.setGoodsId(1L);
        orderItem.setOrderId(1L);
        orderItem.setGoodsPrice(10.0f);
        orderItem.setId(Long.valueOf(count));
        return orderItem;
    }
    
    <T extends Object>List<T> getObjectList(int count,String type){
        List<T> objectList = new ArrayList<>(count);
        for (int i = 0;i<count;i++){
            T obj = null;
            switch (type){
                case "user":
                    objectList.add((T) getUser(i));
                    break;
                case "card":
                    objectList.add((T) getIdCard(i));
                    break;
                case "order":
                    objectList.add((T) getOrder(i));
                    break;
                case "orderItem":
                    objectList.add((T) getOrderItem(i));
                    break;
                case "cardId":
                    objectList.add((T) Long.valueOf(i));
                    break;
            }
        }
        return objectList;
    }
    
    List<OrderItem> getOrderItemList(int count)
    {
        List<OrderItem> orderItemList = new ArrayList<>(count);
        for (int i = 0; i <count ; i++) {
            orderItemList.add(getOrderItem(i));
        }
        return orderItemList;
    }
    @PostMapping("/insertSingleRecordTable")
    public void insertSingleRecordTable() {
        for (int i=0;i<100;i++) {
            transactionService.insertSingleRecordTable(getUser(i), getIdCard(i), getInsertOrderVO(i+1));
        }
    }
    
    @PostMapping("/insertSingleRecord")
    public void insertSingleRecord(int count) {
        for (int i=0;i<count;i++) {
            transactionService.insertSingleRecord(getUser(i), getIdCard(i),getOrder(i),getOrderItem(i));
        }
    }
    
    
    @PostMapping("/batchInsertTable")
    public void batchInsertTable(int count) {
        transactionService.batchInsertTable(getObjectList(count,"user"),
                getObjectList(count,"card"),getObjectList(count,"order"),getObjectList(count,"orderItem"));
    
    }
    
    
    @PostMapping("/batchInsertOrderItem")
    public void batchInsertOrderItem(int count) {
        transactionService.batchInsertOrderItem(getOrderItemList(count));
    }
    
    @PostMapping("/mixoperate1")
    public void mixoperate1() {
        transactionService.mixoperate1(getObjectList(50000, "user"), getObjectList(40300, "cardId"));
        //出现read only操作
//        for (int i = 0;i<20;i++) {
//            transactionService.mixoperate1(getObjectList(50000, "user"), getObjectList(40100, "cardId"));
//        }
    }
    
    @PostMapping("/mixoperate2")
    public void mixoperate2() {
        transactionService.mixoperate2();
    }
    
    @PostMapping("/mixoperate3")
    public void mixoperate3() {
        transactionService.mixoperate3(getObjectList(50000, "user"), getObjectList(50000, "order"));
    }
    
    
    
    @PostMapping("/deleteTable")
    public void deleteTable(){
        transactionService.deleteTable();
    }
    
    @GetMapping("/getTableCount")
    public void getTableCount(){
        transactionService.tableCount();
    }
    
    @GetMapping("queryAllTable")
   public void queryAllTable(){
        transactionService.queryAllTable();
    }
}
